package yuyue.note03;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalUnit;

public class Note03Test {
    @Test
    //LocalDate 获取日期信息。格式为：YYYY-MM-DD
    public void fun1(){
        // 创建指定日期，参数为整型的年月日
        LocalDate of = LocalDate.of(1994, 9, 16);
        System.out.println(of);

        // 获取当前日期
        LocalDate now = LocalDate.now();
        System.out.println(now);

        // 获取日期信息
        System.out.println("年：" + now.getYear());
        System.out.println("月（英语）：" + now.getMonth());
        System.out.println("月：" + now.getMonthValue());
        System.out.println("当月的第几日：" + now.getDayOfMonth());
        System.out.println("周几：" + now.getDayOfWeek());
        System.out.println("当年的第几日：" + now.getDayOfYear());

    }

    @Test
    //LocalTime 获取时间信息。格式为 16:38:54.158549300
    public void fun3(){
        // 指定的时间。时、分、秒、毫秒值
        LocalTime of = LocalTime.of(12, 15, 3, 129_900_000);
        System.out.println(of);

        // 当前时间
        LocalTime now = LocalTime.now();
        System.out.println(now);

        // 获取时间信息
        System.out.println("时" + now.getHour());
        System.out.println("分" + now.getMinute());
        System.out.println("秒" + now.getSecond());
        System.out.println("纳秒" + now.getNano());
    }

    @Test
    // LocalDateTime类: 获取日期时间信息。格式为 2023-03-09T17:19:24.695
    public void fun4(){
        // 指定的时间。
        LocalDateTime of = LocalDateTime.of(2023, 3, 9, 17, 15, 29, 122_133_001);
        System.out.println(of);

        // 当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(now.getYear());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getHour());
        System.out.println(now.getMinute());
        System.out.println(now.getSecond());
    }

    @Test
    //修改时间，使用 withAttribute方法
    public void fun5(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //直接指定
        LocalDateTime localDateTime = now.withYear(2012);
        System.out.println("修改年：" + localDateTime);
        System.out.println(now == localDateTime);

        System.out.println("修改月：" + now.withMonth(1));
        System.out.println("修改日：" + now.withDayOfMonth(1));
        System.out.println("修改时：" + now.withHour(1));
        System.out.println("修改分：" + now.withMinute(1));
        System.out.println("修改秒：" + now.withSecond(1));

        //加减时间 plus 加 minus 减
        System.out.println("修改年：" + now.plusYears(5));
        System.out.println("修改月：" + now.plusMonths(1));
        System.out.println("修改日：" + now.plusDays(1));
        System.out.println("修改时：" + now.plusHours(1));
        System.out.println("修改分：" + now.plusMinutes(1));
        System.out.println("修改秒：" + now.plusSeconds(1));
    }

    @Test
    //比较时间
    public void fun6(){
        LocalDate now = LocalDate.now();
        LocalDate now1 = LocalDate.of(2012, 9, 23);

        System.out.println(now.isAfter(now1));//true
        System.out.println(now.isBefore(now1));//false
        System.out.println(now.equals(now1));//false
    }

    @Test
    //时间格式化。比较坑的一点，格式的大小写有要求。
    public void fun7(){
        LocalDateTime now = LocalDateTime.now();

        //时间转换为固定格式字符串
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
        String format = now.format(dateTimeFormatter);
        System.out.println(format);

        //固定格式字符串转换为时间
        LocalDateTime parse = LocalDateTime.parse(format, dateTimeFormatter);
        System.out.println(parse);

    }

    @Test
    //时间戳
    public void fun8(){
        Instant now = Instant.now();

        System.out.println("当前时间戳=" + now);
        System.out.println("纳秒数=" + now.getNano());
        //从1970-01-01T00:00:00Z开始的秒数
        System.out.println("秒数=" + now.getEpochSecond());
        //从1970-01-01T00:00:00Z开始的毫秒数
        System.out.println("毫秒数=" + now.toEpochMilli());
        System.out.println("毫秒数=" + System.currentTimeMillis());

        Instant instant = Instant.ofEpochSecond(5);
        System.out.println(instant);
    }

    @Test
    //Duration/Period类: 计算日期时间差
    public void fun9(){
        //计算日期
        LocalDate of = LocalDate.of(2023, 1, 1);
        LocalDate now = LocalDate.now();
        Period between = Period.between(of, now);

        System.out.println("年"+between.getYears());
        System.out.println("月"+between.getMonths());
        System.out.println("日"+between.getDays());

        //计算时间
        LocalTime of1 = LocalTime.of(10, 23, 10);
        LocalTime now1 = LocalTime.now();
        Duration between1 = Duration.between(of1, now1);

        System.out.println("日"+between1.toDays());
        System.out.println("时"+between1.toHours());
        System.out.println("分"+between1.toMinutes());
        System.out.println("毫秒"+between1.toMillis());

        System.out.println("秒？"+between1.getSeconds());

    }

    @Test
    //时间校正器
    public void fun10(){
        LocalDateTime now = LocalDateTime.now();

        LocalDateTime next = now.with(temporal -> {
            LocalDateTime localDateTime = (LocalDateTime) temporal;
            LocalDateTime nextMoth = localDateTime.plusMonths(1).withDayOfMonth(1);

            System.out.println(nextMoth);
            return nextMoth;
        });

        System.out.println(next);

    }



}
